<template>
	<view class="pay-page">
		
		<!-- 商品信息 -->
		<view class="shopping-box">
			<view class="shopping-top">
				<view class="lable-title">共{{num}}件商品</view>
			</view>
			<view class="shopping-list">
				<view class="shopping-item" v-for="(item,index) in cartList">
					<view class="shopping-img">
						<image :src="item.products.thump"></image>
					</view>
					<view class="shopping-info">
						<view class="shopping-hd">
							<view class="shopping-title">{{item.products.title}}</view>
							<view class="shopping-price">x1</view>
							<view class="shopping-price">￥{{item.products.price}}</view>
						</view>
						<view class="shopping-text">{{item.products.desc}}</view>
					</view>
				</view>

			</view>
			<view class="cell-list" style="display: flex;align-items: center;justify-content: flex-end;">
				<view class="cell-right">合计
					<text style="font-weight: 900;font-size: 30rpx;">￥{{sumMomey}}</text>
				</view>
			</view>
		</view>
		<!-- 订单信息 -->
		
		<view class="footer-box">
			<view class="footer-content">
				<view class="price-box">
					<view class="price-text2">合计:
						<u-icon name="rmb" :bold="true" color="#000" size="32rpx"></u-icon>{{sumMomey}}
					</view>
				</view>
				<view class="print-item-entry" style="flex:2;">

					<view class="select-box" style="padding:24rpx 60rpx;border-radius: 14rpx;">
						<view class="selecet-text" @click="create">去支付</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import request from '@/common/request.js';
	export default {
		data() {
			return {
				skuIds:"",
				cartList:[
					
				],
				num:0,
				type:'cart'
			}
		},
		computed:{
			sumMomey(){
				let sumPrice = 0;
				let len = this.cartList.length;
				const arr = [];
				for (let i = 0; i < len; i++) {
					sumPrice += this.floor(this.cartList[i].products.price);
				}
				console.log('mon',sumPrice);
				const money = sumPrice.toFixed(2);
				return money;
			}
		},
		onLoad(option) {
			if(!option.ids){
				uni.navigateBack({
					delta:1
				})
				return ;
			}
			
			this.skuIds = option.ids;
			if(option.type == 'now'){
				this.type = option.type
				this.initNow();
			}else{
				this.init();
			}
		},
		methods: {
			init(){
				let opt = {
					url : 'api/gashapons/cartlist',
					method: 'get'
				};
				
				request.httpTokenRequest(opt,{skuIds:this.skuIds}).then((res)=>{
					this.cartList = res.data.data.data;
					this.num = this.cartList.length
				})
			},
			initNow(){
				let opt = {
					url : 'api/gashapons/detail',
					method: 'get'
				};
				
				let that = this;
				
				request.httpTokenRequest(opt,{id:this.skuIds}).then((res)=>{
					let obj = {
						products:{
							id:res.data.data.id,
							thump:res.data.data.picture,
							title:res.data.data.title,
							price:res.data.data.price,
							desc:res.data.data.desc,
						}
					}
					that.cartList = [obj];
					that.num = 1
				})
			},
			// 向下舍去小数点后两位
			floor(val) {
				return Math.floor(val * 100) / 100;
			},
			create(){
				uni.showModal({
					title:'提示',
					content:'确定支付订单吗？',
					success: (e) => {
						if(e.confirm){
							let opt = {
								url : 'api/gashapons/createOrder',
								type:this.type,
								method: 'post'
							};
							
							if(this.type =='now'){
								request.httpTokenRequest(opt,{ids:this.skuIds,type:"now"}).then((res)=>{
									uni.showToast({ title:res.data.msg, icon: 'none' });
									
									setTimeout(()=>{
										uni.navigateTo({
											url:"/pages/me/success?id="+res.data.data.id
										})
									},1000)
									return ;
								})
							}else{
								request.httpTokenRequest(opt,{ids:this.skuIds}).then((res)=>{
									uni.showToast({ title:res.data.msg, icon: 'none' });
									
									setTimeout(()=>{
										uni.navigateTo({
											url:"/pages/me/success?id="+res.data.data.id
										})
									},1000)
									return ;
								})
							}
							
						}
					}	
				})
			},
		}
	}
</script>

<style lang="scss">
	page{
		background: #f2f2f2;
	}
	.pay-page {
		padding: 30rpx 30rpx 160rpx 30rpx;
		display: flex;
		flex-direction: column;

		.address-box {
			padding: 20rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 14rpx;
			background: #fff;
		}

		.address-text {
			font-size: 28rpx;
			color: #000;
			margin-left: 10rpx;
		}

		.shopping-box {
			margin-top: 20rpx;
			border-radius: 14rpx;
			display: flex;
			flex-direction: column;
			background: #fff;
			padding: 20rpx 0;
		}

		.lable-title {
			font-size: 32rpx;
			font-weight: 600;
			color: #000;
			padding: 10rpx 24rpx;
			border-bottom: 2rpx solid rgb(248, 248, 248);
		}

		.shopping-list {
			display: flex;
			flex-direction: column;
			padding: 0 24rpx;

		}

		.lable-number {
			font-size: 24rpx;
			color: #999;
			padding-right: 24rpx;
		}

		.shopping-top {
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.shopping-item {
			display: flex;
			padding: 20rpx 0;
			border-bottom: 2rpx solid rgb(248, 248, 248);
		}

		.shopping-img {
			height: 100rpx;
			width: 100rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.shopping-info {
			flex: 1;
			margin-left: 20rpx;
			display: flex;
			flex-direction: column;
		}

		.shopping-hd {
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.shopping-title {
			flex: 1;

		}

		.shopping-price {
			width: 120rpx;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			color: #000;
			font-size: 28rpx;
			font-weight: 600;
		}

		.shopping-text {
			margin-top: 10rpx;
			font-size: 24rpx;
			color: #999;
		}
	}

	.end-price {
		padding: 24rpx;
		display: flex;
		align-items: center;
		justify-content: flex-end;
	}

	.end-label {
		font-size: 24rpx;
		color: #000;
	}

	.end-number {
		color: rgb(245, 54, 22);
		font-size: 28rpx;
		font-weight: 600;
	}
	.footer-box {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		height: 140rpx;
		background: #fff;
		display: flex;
	border-top: 1rpx solid rgb(214, 215, 217);
		.footer-content {
			padding: 24rpx;
			display: flex;
			align-items: center;
			// margin-bottom: 60rpx;
			flex: 1;
		}
	
		.price-box {
			flex: 1;
			display: flex;
			align-items: center;
		}
	
		.price-text {
			display: flex;
			align-items: center;
			color: rgb(245, 54, 22);
			font-weight: 600;
			font-size: 32rpx;
		}
		.price-text2 {
			display: flex;
			align-items: center;
			color: #000;
			font-weight: 600;
			font-size: 32rpx;
		}
	}
	.print-item-entry {
		display: flex;
		align-items: center;
		// flex: 1;
		flex-wrap: wrap;
		justify-content: flex-end;
	}
	.select-box {
		// width: 200rpx;
		display: flex;
		align-items: center;
		background: #CECECE;
		padding: 10rpx 30rpx;
		border-radius: 10rpx;
		margin-left: 20rpx;
		justify-content: center;
	
		.selecet-text {
			color: #000;
			font-size: 30rpx;
		}
	
		.selecet-icon {
			margin-left: 4rpx;
		}
	}
	.cell-left {
		font-size: 24rpx;
		color:#000;
	}
	.cell-list {
		padding: 30rpx 24rpx;
		
	}
</style>